package com.luhuiguo.cms.client.presenter;

import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FormEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FileUploadField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.core.client.GWT;
import com.google.inject.Inject;
import com.luhuiguo.cms.client.GlobalConstants;
import com.luhuiguo.cms.client.GlobalMessages;
import com.luhuiguo.cms.shared.event.AttachmentUploadedEvent;
import com.luhuiguo.cms.shared.event.AttachmentUploadedEventHandler;
import com.luhuiguo.cms.shared.event.EditAttachmentCancelledEvent;
import com.luhuiguo.cms.shared.event.EditAttachmentCancelledEventHandler;
import com.luhuiguo.cms.shared.model.AttachmentModel;
import com.luhuiguo.gwt.dispatcher.client.DispatcherAsync;
import com.luhuiguo.gwt.presenter.client.EventBus;
import com.luhuiguo.gwt.presenter.client.widget.WidgetDisplay;
import com.luhuiguo.gwt.presenter.client.widget.WidgetPresenter;

public class EditAttachmentPresenter extends WidgetPresenter<EditAttachmentPresenter.Display> {

    public interface Display extends WidgetDisplay {

        FormPanel getFormPanel();

        FormBinding getFormBinding();

        FileUploadField getFileField();

        TextField<String> getTitleField();

        Button getSubmitButton();

        Button getCancelButton();

        void show();

        void hide();
    }

    @SuppressWarnings("unused")
    private DispatcherAsync dispatcher;

    @SuppressWarnings("unused")
    private GlobalConstants constants;

    private GlobalMessages messages;

    private AttachmentModel attachmentModel;

    @Inject
    public EditAttachmentPresenter(Display display, EventBus eventBus, DispatcherAsync dispatcher,
            GlobalConstants constants, GlobalMessages messages) {
        super(display, eventBus);
        this.dispatcher = dispatcher;
        this.constants = constants;
        this.messages = messages;

    }

    @Override
    protected void onBind() {
        GWT.log("binding EditAttachmentPresenter");

        registerHandler(eventBus.addHandler(AttachmentUploadedEvent.TYPE, new AttachmentUploadedEventHandler() {
            @Override
            public void onAttachmentUploaded(AttachmentUploadedEvent event) {
                display.hide();
            }
        }));

        registerHandler(eventBus.addHandler(EditAttachmentCancelledEvent.TYPE,
                new EditAttachmentCancelledEventHandler() {

                    @Override
                    public void onEditAttachmentCancelled(EditAttachmentCancelledEvent event) {
                        display.hide();
                    }

                }));

        display.getFormPanel().addListener(Events.Submit, new Listener<FormEvent>() {

            @Override
            public void handleEvent(FormEvent be) {

                // Window.alert(be.getResultHtml());

                if (be.getResultHtml().contains("success")) {
                    eventBus.fireEvent(new AttachmentUploadedEvent(display.getFileField().getValue()));
                } else {
                    MessageBox.alert(messages.error(), messages.uploadAttachmentFailed(), null);
                }

            }

        });

        display.getFileField().addListener(Events.OnChange, new Listener<BaseEvent>() {

            @Override
            public void handleEvent(BaseEvent be) {

                if (null == display.getTitleField().getValue()) {

                    display.getTitleField().setValue(display.getFileField().getValue());
                }
            }

        });

        display.getCancelButton().addSelectionListener(new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                eventBus.fireEvent(new EditAttachmentCancelledEvent(attachmentModel));
            }
        });

        display.getSubmitButton().addSelectionListener(new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                if (display.getFormPanel().isValid()) {
                    // display.getFormPanel().mask();

                    display.getFormPanel().submit();

                }
            }
        });

        // display.getFormPanel().set

    }

    @Override
    protected void onRevealDisplay() {
    }

    @Override
    protected void onUnbind() {

    }

    public void revealDisplay(AttachmentModel attachmentModel) {
        this.attachmentModel = attachmentModel;

        BeanModelFactory factory = BeanModelLookup.get().getFactory(AttachmentModel.class);
        BeanModel model = factory.createModel(attachmentModel);

        display.getFormBinding().bind(model);

        display.show();

        revealDisplay();
    }

}
